using System;
using System.Collections.Generic;

using Gtk;
using Gdk;

using Dashboard.Mint.Tiles;
using DMA = Dashboard.Mint.Actions;

namespace Dashboard.Latte {

	public class Sidebar : VBox {

		private SidebarGroup actions_group = null;

		public Sidebar () : base (true, 10)
		{			
			base.AppPaintable = true;
			base.BorderWidth = 10;

			actions_group = new SidebarGroup ("Actions");
			//actions_group.NoShowAll = true;

			PackStart (actions_group, false, true, 0);
		}

		public void Update (Tile tile)
		{
			//actions_group.Hide ();
			actions_group.Clear ();

			foreach (DMA.Action action in DMA.ActionFactory.BuildActions (tile.Match)) {
				actions_group.AddAction (new ActionItem (action, tile));
			}

			actions_group.ShowAll ();
		}

		protected override bool OnExposeEvent (Gdk.EventExpose evnt)
		{
			if (!IsDrawable)
				return true;

			Cairo.Context gr = Gdk.CairoHelper.Create (evnt.Window);

			Gdk.Color dark = Style.DarkColors [(int)StateType.Normal];
			Gdk.Color mid = Style.MidColors [(int)StateType.Normal];

			// Fill
			gr.Rectangle (Allocation.X, Allocation.Y, Allocation.Width, Allocation.Height);
			Cairo.Gradient pattern = new Cairo.LinearGradient (Allocation.X, Allocation.Y, Allocation.X + Allocation.Width, Allocation.Y);
			pattern.AddColorStop (0, CairoFu.GdkColorToCairoColor (mid));
			pattern.AddColorStop (1, CairoFu.GdkColorToCairoColor (dark));
			gr.Pattern = pattern;
			gr.Fill ();

			// Border
			gr.MoveTo (Allocation.X + 0.5, Allocation.Y + 0.5);
			gr.LineTo (Allocation.X + 0.5, Allocation.Y + Allocation.Height - 0.5);
			gr.LineWidth = 0.5;
			gr.Color = CairoFu.GdkColorToCairoColor (dark);
			gr.Stroke ();
			
			CairoFu.DisposeContext (gr);
			
			return base.OnExposeEvent (evnt);
		}

		protected override void OnSizeRequested (ref Requisition requisition)
		{
			base.OnSizeRequested (ref requisition);			
			requisition.Width = Math.Max (requisition.Width, 150);
		}
	}
}